package com.ttbj.product.dispatch.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ttbj.api.dispatch.DispatchService;
import com.ttbj.api.dto.dispatch.DispatchDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.constant.PlatFormErrorCode;
import com.ttbj.constant.annotation.SystemServiceLog;
import com.ttbj.core.util.DtoTransitionUtils;
import com.ttbj.dispatchtask.bean.po.Dispatch;
import com.ttbj.dispatchtask.dao.DispatchMapper;

@Service("dispatchService")
public class DispatchServiceImpl implements DispatchService{
	
	@Autowired
	private DispatchMapper dispatchMapper;

	@Override
	public DispatchDto findDispatchInfo() throws BusinessException {
		Dispatch dispatch = this.dispatchMapper.queryDispatchInfo(); 
		DispatchDto dispatchDto = (DispatchDto) DtoTransitionUtils.trans(DispatchDto.class, dispatch);
		return dispatchDto;
	}

	@Override
	@Transactional
	@SystemServiceLog(description="修改手动/自动派单配置")
	public void editDispatchInfo(DispatchDto dispatchDto) throws BusinessException {
		if (null == dispatchDto) {
			throw new BusinessException("数据有错误！", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		Dispatch dispatch = (Dispatch) DtoTransitionUtils.trans(Dispatch.class, dispatchDto);
		dispatch.setDisUpdateTime(new Date());	//设置更新时间
		//执行更改操作
		int result = this.dispatchMapper.updateDispatchInfo(dispatch);
		//如果更新结果返回0，则返回提示信息
		if (result == 0) {
			throw new BusinessException("更改失败，重新更改", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
	}
	
}
